﻿using System;
using System.Collections.Generic;
using System.Text;

using Torch.WL.DataAccess.CleanChina;
using Torch.WL.Entity.CleanChina;
using Torch.FrameWork.Utility;

namespace Torch.WL.Business.CleanChina
{
    class TbCompanyManager
    {
        private readonly TbCompanyAccess dal = new TbCompanyAccess();

        private static TbCompanyManager _companyManager;
        public static TbCompanyManager Instance()
        {
            if (null == _companyManager )
                _companyManager = new TbCompanyManager();

            return _companyManager;
        }

        private TbCompanyManager()
        {         
        }
        /*
        public List<CompanyProvider.ResponseDataInfo> QueryCompany(string querykey, int queryby)
        {
            List<CompanyProvider.ResponseDataInfo> list = new List<CompanyProvider.ResponseDataInfo>();


            Dictionary<string, List<CompanyProvider.ResponseDataInfo.companyinfo>> dict = new Dictionary<string, List<CompanyProvider.ResponseDataInfo.companyinfo>>();

            List<TbCompanyEntity> listcompanyentity =  dal.QueryCompany(querykey, queryby);

            foreach (TbCompanyEntity entity in listcompanyentity)
            {
                if (!dict.ContainsKey(entity.firstPingYinChar))
                {
                    dict.Add(entity.firstPingYinChar, new List<CompanyProvider.ResponseDataInfo.companyinfo>());
                }           
                dict[entity.firstPingYinChar].Add(new CompanyProvider.ResponseDataInfo.companyinfo() { companyid = entity.companyId, companyname = entity.companyName });
            }


            foreach (KeyValuePair<string, List<CompanyProvider.ResponseDataInfo.companyinfo>> kvp in dict)
            {
                list.Add(new CompanyProvider.ResponseDataInfo() { firstchar=kvp.Key, company=kvp.Value});                
            }

            return list;


        }
    */

        public List<CompanyProvider.ResponseDataInfo> QueryCompanyByGroup(string querykey, int sortby)
        {
            List<CompanyProvider.ResponseDataInfo> list = new List<CompanyProvider.ResponseDataInfo>();


            Dictionary<string, List<CompanyProvider.ResponseDataInfo.companyinfo>> dict = new Dictionary<string, List<CompanyProvider.ResponseDataInfo.companyinfo>>();

            List<TbCompanyEntity> listcompanyentity = dal.QueryCompany(querykey, sortby);

            foreach (TbCompanyEntity entity in listcompanyentity)
            {
                if (sortby == 1)
                {
                    if (!dict.ContainsKey(entity.firstPingYinChar))
                    {
                        dict.Add(entity.firstPingYinChar, new List<CompanyProvider.ResponseDataInfo.companyinfo>());
                    }
                    dict[entity.firstPingYinChar].Add(new CompanyProvider.ResponseDataInfo.companyinfo() { companyid = entity.companyId, companyname = entity.companyName });
                }
                else
                    if (sortby == 2)
                    {
                        if (!dict.ContainsKey(entity.productTypeName))
                        {
                            dict.Add(entity.productTypeName, new List<CompanyProvider.ResponseDataInfo.companyinfo>());
                        }
                        dict[entity.productTypeName].Add(new CompanyProvider.ResponseDataInfo.companyinfo() { companyid = entity.companyId, companyname = entity.companyName });

 
                    }


            }


            foreach (KeyValuePair<string, List<CompanyProvider.ResponseDataInfo.companyinfo>> kvp in dict)
            {
                list.Add(new CompanyProvider.ResponseDataInfo() { sectionname = kvp.Key, company = kvp.Value });
            }

            return list;

        }



        public List<TbCompanyEntity> GetRewardCompanyByCache(int rewardId)
        {
            string CacheKey = "TbCompanyEntityRewardCompany-" + rewardId;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetRewardCompany(rewardId);
                    if (objModel != null)
                    {
                        int ModelCache = ConfigHelper.GetConfigInt("EntityCacheMinutes");
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (List < TbCompanyEntity > )objModel;
        }


        public TbCompanyEntity GetModel(int expoId)
        {

            return dal.GetModel(expoId);
        }

        public TbCompanyEntity GetModelByCache(int companyId)
        {

            string CacheKey = "TbCompanyEntityModel-" + companyId;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(companyId);
                    if (objModel != null)
                    {
                        int ModelCache = ConfigHelper.GetConfigInt("EntityCacheMinutes");
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (TbCompanyEntity)objModel;
        }



       



    }
}
